package com.lanxiao.cloud.user.api.fill;

import com.lanxiao.cloud.common.core.dto.BaseDto;
import com.lanxiao.cloud.common.core.dto.UserBaseDto;
import com.lanxiao.cloud.common.fill.FillAppointment;
import com.lanxiao.cloud.common.fill.FillHandle;
import com.lanxiao.cloud.common.fill.FillHandleFactory;
import com.lanxiao.cloud.common.fill.FillType;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2021-09-16
 */
@Component
public class BaseDtoFillHandleFactory<I> {

  private final FillHandleFactory fillHandleFactory;

  public BaseDtoFillHandleFactory(FillHandleFactory fillHandleFactory) {
    this.fillHandleFactory = fillHandleFactory;
  }

  public <T extends BaseDto<I>> void fillHandle(List<T> list) {
    List<FillAppointment<T, UserBaseDto<I>>> fillAppointments = new ArrayList<>();
    List<Function<T,I>> functions = new ArrayList<>();
    fillAppointments.add(T::setCreateUser);
    fillAppointments.add(T::setUpdateUser);
    functions.add(o->o.getCreateUser().getId());
    functions.add(o->o.getUpdateUser().getId());
    fillHandleFactory.fill(list, new FillHandle<>(FillType.USER_INFO, fillAppointments, functions));
  }

}
